﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;

namespace CSharpXDL
{
    class ConnectHelper
    {
        static Object l=new Object();
        //  Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);   
     /*   public static HttpConnect GetHttpConnect(String url, String heads, long range)
        {
            Uri uri = new Uri(url);
            String host = uri.Host;
            int port = uri.Port;
            Socket socket = ConnectSocket(host, port, ProtocolType.Tcp);
         
            HttpConnect con = new HttpConnect(socket,range);
          
            con.uri = uri;
            return con;
        }*/

        [MethodImpl(MethodImplOptions.Synchronized)]
        public static XDLConnect GetXDLConnect(String url, long range) {

            Uri uri = new Uri(url);
            if (uri.Scheme == Uri.UriSchemeFtp)
                return GetFtpConnect(url, range);
            if (uri.Scheme == Uri.UriSchemeHttp)
                return GetHttpConnect(url, range);

            return null;
        }


        private static FtpConnect GetFtpConnect(String url, long range)
        {
            Uri uri = new Uri(url);
            String host = uri.Host;
            int port = uri.Port;
            Socket socket = ConnectSocket(host, port, ProtocolType.Tcp);

            return new FtpConnect(url, range, socket);

        }
        private static HttpConnect GetHttpConnect(String url, long range)
        {
            

                Uri uri = new Uri(url);
                String host = uri.Host;
                int port = uri.Port;
                Socket socket = ConnectSocket(host, port, ProtocolType.Tcp);
                HttpConnect con = new HttpConnect(socket, range);
                con.init(uri);
                return con;
           
        }
         [MethodImpl(MethodImplOptions.Synchronized)]
        internal static Socket ConnectSocket(string server, int port, ProtocolType protocoltype)
        {
            Socket s = null;
            IPHostEntry hostEntry = null;

            // Get host related information.
            hostEntry = Dns.GetHostEntry(server);

            // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
            // an exception that occurs when the host IP Address is not compatible with the address family
            // (typical in the IPv6 case).
            foreach (IPAddress address in hostEntry.AddressList)
            {
                IPEndPoint ipe = new IPEndPoint(address, port);
                Socket tempSocket =
                    new Socket(ipe.AddressFamily, SocketType.Stream, protocoltype);

                tempSocket.Connect(ipe);

                if (tempSocket.Connected)
                {
                    s = tempSocket;
                    break;
                }
                else
                {
                    continue;
                }
            }
            return s;
        }
    }
}
